package com.nbjtjc.safe.service.cost;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostAccountRecordMapper;
import com.nbjtjc.safe.mapper.cost.CostAccountRegMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetItemMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetSubItemMapper;
import com.nbjtjc.safe.model.cost.CostAccountRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CostAccountRecordService {
	@Autowired
	  private CostAccountRecordMapper costAccountRecordMapper;
	@Autowired
	  private CostAccountRegMapper costAccountRegMapper;
	@Autowired
	  private CostBudgetSubItemMapper costBudgetSubItemMapper;
	@Autowired
	  private CostBudgetItemMapper costBudgetItemMapper;
	
	  @Transactional(rollbackFor = {Throwable.class})
	  public Result add(CostAccountRecord costAccountRecord){
		        Result result=new Result();
		        if(ObjectUtil.isNull(costAccountRecord)
		        		||ObjectUtil.isNull(costAccountRecord.getBudgetItemId())
		        		||ObjectUtil.isNull(costAccountRecord.getAccountRegId())
		        		){		            
		        	result.setFalse("关联预算投入ID和预算计划子项目ID不能为空");
		            return result;
		        }
		        //主键为空或库中无此主键则为新建，插入数据
		        if(ObjectUtil.isNull(costAccountRecord.getId())|| ObjectUtil.isNull(costAccountRecordMapper.selectByPrimaryKey(costAccountRecord.getId()))){
		        	int res=costAccountRecordMapper.insertSelective(costAccountRecord);
			        if(res==0){
			            result.setFalse("插入费用投入失败");
			            return result;
			        }
			        
			        //更新预算投入金额
			        res = costAccountRegMapper.updateTotalAccount(costAccountRecord.getAccountRegId());
			        if(res==0){
			            result.setFalse("更新费用投入总额失败");
			            return result;
			        }
//			    	result = updateItemByRecord(costAccountRecord.getAccountRegId());
//		        	 if(result.isSuccess()==false){
//		        		 result.setFalse("更新预算投入项目失败");
//				         return result;
//		        	 }
//			        
			        //更新预算项目和子项目费用投入金额
		        	 result = updateItemByRecord(costAccountRecord.getBudgetItemId());
		        	 if(result.isSuccess()==false){
	        		 result.setFalse("更新预算投入项目失败");
			         return result;
		        	 }

		        }else
		        {
		        	int oldSubItemId = costAccountRecord.getBudgetItemId();
		        	
		        	int res=costAccountRecordMapper.updateByPrimaryKeySelective(costAccountRecord);
		        	 if(res==0){
		        		 result.setFalse("更新费用投入失败");
				         return result;

		        	 }
				        //更新预算投入金额
				        res = costAccountRegMapper.updateTotalAccount(costAccountRecord.getAccountRegId());
				        if(res==0){
				            result.setFalse("更新费用投入总额失败");
				            return result;
				        }
//				    	result = updateItemByRecord(costAccountRecord.getAccountRegId());
//			        	 if(result.isSuccess()==false){
//			        		 result.setFalse("更新预算投入项目失败");
//					         return result;
//			        	 }
				        
				        //更新预算项目和子项目费用投入金额,修改前和修改后的子项目都需更新
			        	 result = updateItemByRecord(oldSubItemId);
			        	 if(result.isSuccess()==false){
		        		 result.setFalse("更新预算投入项目失败");
				         return result;
			        	 }
			        	 result = updateItemByRecord(costAccountRecord.getBudgetItemId());
			        	 if(result.isSuccess()==false){
		        		 result.setFalse("更新预算投入项目失败");
				         return result;
			        	 }
		        }
		        result.setData(costAccountRecord);

		        return result;
		    }

	  //更新某预算费用所有项目和子项目的投入金额，根据预算计划Id
	  @Transactional(rollbackFor = {Throwable.class})
	  public Result updateAllItemAccount(int planId){
	      Result result=new Result();
	      int res=-1;
	      if(ObjectUtil.isNull(planId)||planId==-1){
		        return result;
	      }
		  //更新预算子项目投入总额
	      res= costBudgetSubItemMapper.updateAccountSumByPlanId(planId);
	      if(res==0){
	    	  result.setFalse("更新子项目投入总额失败");
		      return result;
	      }
	      
	      //更新预算一级项目投入总额
	      res= costBudgetItemMapper.updateAccountSumByPlanId(planId);
	      if(res==0){
	    	  result.setFalse("更新子项目投入总额失败");
		      return result;
	      }
	      
	      return result;
	  }
	  
	  
	  //更新相关预算项目和子项目投入金额,根据单个子项目ID
	  public Result updateItemByRecord(int subItemId){
	      Result result=new Result();
		  int res=-1;
		  //更新预算子项目投入总额
		 res = costBudgetSubItemMapper.updateAccountSum(subItemId);
	      if(res==0){
	    	  result.setFalse("更新子项目投入总额失败");
		      return result;
	      }
	    	//更新预算一级项目投入总额
		 res =  costBudgetItemMapper.updateAccountSum(subItemId);
	      if(res==0){
	    	  result.setFalse("更新预算项目投入总额失败");
		      return result;
	      }
	      return result;
	  }

	  @Transactional(rollbackFor = {Throwable.class})
	  public Result delete(CostAccountRecord costAccountRecord){
	      Result result=new Result();
	      if(ObjectUtil.isNull(costAccountRecord.getId())){
	          result.setFalse("作废对象不能为空");
	          return result;
	      }
      
	      int id =costAccountRecord.getId();
	      costAccountRecord = costAccountRecordMapper.selectByPrimaryKey(id);
	      int regId=costAccountRecord.getAccountRegId();
	      int costBudgetSubItemId=costAccountRecord.getBudgetItemId();
//0906	
//	      costAccountRecord.setCancel(1);
//	      int res = costAccountRecordMapper.updateByPrimaryKeySelective(costAccountRecord);
//0906	      
	      int res = costAccountRecordMapper.deleteByPrimaryKey(id);
	      if(res==0){
	    	  result.setFalse("作废失败");
		      return result;
	
	      }
	      //更新费用投入总额
	      res = costAccountRegMapper.updateTotalAccount(regId);
	        if(res==0){
	            result.setFalse("更新费用投入总额失败");
	            return result;
	        }
		  //更新相关预算项目和子项目投入金额
	      result = updateItemByRecord(costBudgetSubItemId);
     	 if(result.isSuccess()==false){
 		 result.setFalse("更新预算投入项目失败");
	         return result;
     	 }
	      return result;
	  }

	  
	  public Result findByRegId(CostAccountRecord costAccountRecord) {
	      Result result = new Result();
	      if(ObjectUtil.isNull(costAccountRecord)||ObjectUtil.isNull(costAccountRecord.getAccountRegId())){
	    	  result.setFalse("RegId为空");
	    	  return result;
	      }
	      PageHelper.startPage(costAccountRecord.getPageCurrent(), costAccountRecord.getPageSize());

	      List<CostAccountRecord> costAccountRecords = costAccountRecordMapper.findByRegId(costAccountRecord.getAccountRegId());
//	      List<CostAccountRecord> costAccountRecords = costAccountRecordMapper.select(costAccountRecord);

	      result.setData(new PageInfo<>(costAccountRecords));

	      return result;
	  }
}
